package com.bishe.demo.core.auth;

import com.bishe.demo.core.entity.Manager;
import com.bishe.demo.core.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    private TestService testService;
    private PasswordEncoder pw=new BCryptPasswordEncoder();


    @Override
    public UserDetails loadUserByUsername(String id) throws UsernameNotFoundException {
        Manager manager=testService.getById(id);
        if(manager==null){
            throw new UsernameNotFoundException("用户名不存在");
        }
        return new User(id,pw.encode(manager.getPassword()),true,true,true,true, AuthorityUtils.commaSeparatedStringToAuthorityList(manager.getRole()));
    }
}
